<script setup lang="ts">
import { showToast } from "vant";
import { $t } from "@/locales";
import { getHtmlBuildTime, jsonParseItem, getQueryString, isiOS, isAndroid, isHarmony, isWeixin } from "@/utils";
import useAppStore from "@/store/modules/app";
import { navigationBarHeight, getUserInfo } from "@/utils/bridge";

const appStore = useAppStore();
const statusBarHeight = jsonParseItem('statusBarHeight');
if (statusBarHeight) {
  appStore.statusBarHeight = statusBarHeight;
}
navigationBarHeight(function (res: any) {
  appStore.statusBarHeight = Math.abs(res.StatusBarHeight);
  localStorage.setItem('statusBarHeight', appStore.statusBarHeight.toString());
});

defineOptions({
  name: 'App'
});

onMounted(() => {
  window.addEventListener('offline', handleOffline);
  window.addEventListener('online', handleOnline);
  document.body.addEventListener('click', function (event) {
    if (navigator.onLine) {
      // 网络连接正常
    } else {
      // 网络已断开
      showToast({ message: $t('common.networkOff'), duration: 1000, forbidClick: true });
    }
  }, true);
  locationReload();
});

onUnmounted(() => {
  localStorage.removeItem('mine-my-message-type');
  localStorage.removeItem('mine-my-message-time');
  window.close();
})

async function locationReload() {
  const hasReload = sessionStorage.getItem('location-reload'); // 只刷新一次，防止不断刷新
  if (hasReload != '1' && !import.meta.env.DEV) {
    const buildTime = await getHtmlBuildTime();
    if (buildTime !== BUILD_TIME) {
      sessionStorage.setItem('location-reload', '1');
      // @ts-ignore
      location.reload(true);
    }
  }
}

function handleOffline() {
  showToast({ message: $t('common.networkOff'), duration: 1000, forbidClick: true });
}
function handleOnline() {
  showToast($t('common.networkOn'));
  // @ts-ignore
  location.reload(true);
}

onUnmounted(() => {
  window.removeEventListener('offline', handleOffline);
  window.removeEventListener('online', handleOffline);
});

</script>

<template>
  <RouterView />
</template>

<style></style>
